package com.bishe.app.modules.menu.controller;

import com.bishe.app.core.api.CommonResult;
import com.bishe.app.core.api.CommonPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.app.core.enums.IsDel;
import com.bishe.app.modules.menu.entity.MenuTree;
import com.bishe.app.modules.menu.valid.MenuValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bishe.app.modules.menu.service.MenuService;
import com.bishe.app.modules.menu.entity.Menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public MenuService menuService;

    @Resource
    private MenuValidator validator;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody Menu menu) {
        validator.validCreate(menu);
        boolean success = menuService.save(menu);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody Menu menu) {
        menu.setId(id);
        boolean success = menuService.updateById(menu);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = menuService.removeById(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/deleteRecursion/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBatch(@PathVariable Long id) {
        List<Long> allMenuId = Optional.ofNullable(menuService.getAllMenuId(id))
                .orElse(new ArrayList<>());
        allMenuId.add(id);

        boolean success = menuService.lambdaUpdate()
                .in(Menu::getId, allMenuId)
                .set(Menu::getIsDel, IsDel.Y.getCode())
                .update();
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Menu>> listAll() {
        List<Menu> menuList = menuService.list();
        return CommonResult.success(menuList);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<Menu>> page(@RequestBody CommonPage commonPage) {
        Page<Menu> page = new Page(commonPage.getPageNum(), commonPage.getPageSize());
        return CommonResult.success(CommonPage.restPage(menuService.page(page)));
    }


    @RequestMapping(value = "/listTree", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<MenuTree>> listTree() {
        List<Menu> list = menuService.lambdaQuery()
                .eq(Menu::getIsDel, IsDel.N.getCode())
                .list();

        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(MenuTree.getTree(list));
    }

}



